﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xn;
using System.Windows.Forms;

namespace KinectCore.Gui.Generator
{
    /// <summary>
    /// This class doesn't work yet!
    /// 
    /// It's supposed to be a simulator, which pretends to be an OpenNI HandsGenerator and allows you to simulate
    /// a hand moving using the mouse (movement only, no clicks)
    /// </summary>
    public class MouseHandsGenerator
    {
        public event xn.HandsGenerator.HandCreateHandler HandCreate;
        public event xn.HandsGenerator.HandDestroyHandler HandDestroy;
        public event xn.HandsGenerator.HandUpdateHandler HandUpdate;

        private Control _parentControl;
        public Control ParentControl
        {
            get { return _parentControl; }
            set
            {
                // remove listeners on old control
                if (_parentControl != null)
                {
                    _parentControl.MouseEnter -= new EventHandler(control_MouseEnter);
                    _parentControl.MouseLeave -= new EventHandler(control_MouseLeave);
                    _parentControl.MouseMove -= new MouseEventHandler(control_MouseMove);
                }

                this._parentControl = value;

                if (_parentControl != null)
                {
                    // add listeners for new control
                    _parentControl.MouseEnter += new EventHandler(control_MouseEnter);
                    _parentControl.MouseLeave += new EventHandler(control_MouseLeave);
                    _parentControl.MouseMove += new MouseEventHandler(control_MouseMove);
                }

            }
        }

        private uint handCount = 0;
        private List<uint> activeHands = new List<uint>();

        public MouseHandsGenerator(Context ctx)
        {
        }

        void control_MouseMove(object sender, MouseEventArgs e)
        {
            updateHand(activeHands[0], new Point3D(e.X - 500, 500 - e.Y , 0f));
        }

        void control_MouseLeave(object sender, EventArgs e)
        {
            // remove any existing hands
            while (activeHands.Count > 0)
            {
                removeHand(activeHands[0]);
            }
        }

        void control_MouseEnter(object sender, EventArgs e)
        {
            // remove any existing hands
            while (activeHands.Count > 0)
            {
                removeHand(activeHands[0]);
            }

            addHand(new Point3D(0f, 0f, 0f));
        }

        public void StartTracking(ref Point3D pos)
        {
            // meh, do nothing
        }

        private void addHand(Point3D point)
        {
            // new hand number
            handCount++;

            activeHands.Add(handCount);

            HandCreate(null, handCount, ref point, 0f);
        }

        private void removeHand(uint hand_id)
        {
            HandDestroy(null, hand_id, 0f);
            activeHands.Remove(hand_id);
        }

        private void updateHand(uint hand_id, Point3D pos)
        {
            HandUpdate(null, hand_id, ref pos, 0f);
        }


        internal void StartGenerating()
        {
            // do nothing
        }
    }
}
